/**
 * 
 */
package com.yidu.drug.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.AuditModelMapper;
import com.yidu.drug.dao.FdPoBreakdownModelMapper;
import com.yidu.drug.dao.FdPurchaseModelMapper;
import com.yidu.drug.dao.QtSumModelMapper;
import com.yidu.drug.dao.QualityTestingModelMapper;
import com.yidu.drug.dao.ZgsSellDetailModelMapper;
import com.yidu.drug.dao.ZgsSellOrderModelMapper;
import com.yidu.drug.model.AuditModel;
import com.yidu.drug.model.FdPoBreakdownModel;
import com.yidu.drug.model.FdPurchaseModel;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.model.ZgsSellDetailModel;
import com.yidu.drug.model.ZgsSellOrderModel;
import com.yidu.drug.service.ZgsSellOrderService;

/**
 * 总公司销售订单Service接口实现类
 * @author tanglei
 * @date 2018年8月9日 上午11:40:51
 * @version 1.0
 */
@Service
public class ZgsSellOrderServiceImpl implements ZgsSellOrderService{
	
	//注入总公司销售订单
	@Resource
	private ZgsSellOrderModelMapper zgsSellOrderDao;
	//注入审核Dao
	@Resource
	private AuditModelMapper auditDao;
	//注入分店采购订单Dao
	@Resource
	private FdPurchaseModelMapper fdPurchaseDao;
	//注入分店采购订单详情Dao
	@Resource
	private FdPoBreakdownModelMapper fdPoBreakdownDao;
	//注入质检总报表Dao
	@Resource
	private QtSumModelMapper qtSumDao;
	//注入质检表Dao
	@Resource
	private QualityTestingModelMapper qualityTestingDao;
	//注入总公司销售订单详情Dao
	@Resource
	private ZgsSellDetailModelMapper zgsSellDetailDao;
	
	/**
	 * 根据销售订单ID删除数据
	 * @param soId 传入一个销售订单ID
	 * @return 返回一个影响的行数
	 */
	@Override
	public int deleteByPrimaryKey(String soId) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 增加销售订单数据(一次性增加所有字段)
	 * @param record 传入一个总公司销售订单对象
	 * @return 返回一个影响的行数
	 */
	@Override
	public int insert(ZgsSellOrderModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 增加销售订单数据(可增加一个或多个字段)
	 * @param record 传入一个销售订单对象
	 * @return 返回一个影响的行数
	 */
	@Override
	public int insertSelective(ZgsSellOrderModel record) {
		//调用增加的方法并返回
		return zgsSellOrderDao.insertSelective(record);
	}

	/**
	 * 根据销售订单ID查询销售订单对象
	 * @param soId 传入一个销售订单对象
	 * @return 返回一个销售订单对象
	 */
	@Override
	public ZgsSellOrderModel selectByPrimaryKey(String soId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据销售订单ID修改销售订单数据(可修改一个或多个字段)
	 * @param record 传入一个销售订单对象
	 * @return 返回一个影响的行数
	 */
	@Override
	public int updateByPrimaryKeySelective(ZgsSellOrderModel record) {
		// TODO Auto-generated method stub
		return zgsSellOrderDao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据销售订单ID修改销售订单数据(一次性修改所有字段)
	 * @param record 传入一个销售订单对象
	 * @return 返回一个影响的 行数
	 */
	@Override
	public int updateByPrimaryKey(ZgsSellOrderModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 查询所有销售订单数据(包括根据条件查询和分页)
	 * @param zgsSellOrderModel 传入一个销售订单对象
	 * @return 返回一个集合
	 */
	@Override
	public Map<String,Object> findAll(ZgsSellOrderModel zgsSellOrderModel) {
		//创建一个分页对象
		Page page = new Page();
		//给分页对象的页数属性赋值，数值为销售订单对象里面的页数数值
		page.setPage(Integer.valueOf(zgsSellOrderModel.getPage()));
		//给分页对象的每页显示的行数属性赋值，数值为销售订单对象里面的每页显示行数的数值
		page.setRows(Integer.valueOf(zgsSellOrderModel.getLimit()));
		//给销售订单对象里的分页对象赋值
		zgsSellOrderModel.setPages(page);
		//调用查询所有的方法，并用集合接收
		List<ZgsSellOrderModel> list = zgsSellOrderDao.findAll(zgsSellOrderModel);
		
		//得到一个总行数
		Long count = zgsSellOrderDao.findRows(zgsSellOrderModel);
		//创建一个map集合
		Map<String,Object> map = new HashMap<String, Object>();
		//给map集合赋值
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		//返回
		return map;
	}

	/**
	 * 查询销售订单总行数(包括根据条件 查询)
	 * @param zgsSellOrderModel 传入一个销售订单对象
	 * @return 返回一个总行数
	 */
	@Override
	public long findRows(ZgsSellOrderModel zgsSellOrderModel) {
		//调用查询总行数的方法并返回
		return zgsSellOrderDao.findRows(zgsSellOrderModel);
	}

	@Override
	public void sellSh(AuditModel audit,String condition) {
		ZgsSellOrderModel zgsSellOrder = new ZgsSellOrderModel();
		if("1".equals(audit.getAuditPass())){
			audit.setAuditPass("通过");
			if("2".equals(condition)){
				zgsSellOrder.setIsva("3");
			}else if("3".equals(condition)){
				zgsSellOrder.setIsva("4");
				QtSumModel qtSum = new QtSumModel();
				String sumId = Tools.getRandomUUID();
				qtSum.setSumId(sumId);
				qtSum.setPoId(audit.getQwId());
				qtSum.setSumNopass(0);
				FdPurchaseModel fdPurchase = fdPurchaseDao.selectByPrimaryKey(audit.getQwId());
				if(fdPurchase != null){
					qtSum.setFilId(fdPurchase.getFilId());
				}
				int sumSumNum = zgsSellOrderDao.selectByPrimaryKey(audit.getQwId()).getSoTotalnum().intValue();
				qtSum.setSumSumnum(Integer.valueOf(sumSumNum));
				qtSum.setSumRate(BigDecimal.valueOf(0));
				qtSum.setSumRemark("未质检");
				qtSumDao.insertSelective(qtSum);
				
				List<FdPoBreakdownModel> list = fdPoBreakdownDao.findByCgId(audit.getQwId());
				for(FdPoBreakdownModel fdPoBreakdown:list){
					QualityTestingModel qualityTesting = new QualityTestingModel();
					String qtId = Tools.getRandomUUID();
					qualityTesting.setQtId(qtId);
					qualityTesting.setSumId(sumId);
					qualityTesting.setNopassNum(0);
					qualityTesting.setQtSumnum(fdPoBreakdown.getFdItemNumber().intValue());
					qualityTesting.setQtRate(BigDecimal.valueOf(0));
					qualityTesting.setIsva(Tools.ISVA_YES);
					qualityTesting.setPwId(fdPoBreakdown.getDrugId());
					qualityTestingDao.insertSelective(qualityTesting);
				}
			}
		}else if("0".equals(audit.getAuditPass())){
			audit.setAuditPass("不通过");
			zgsSellOrder.setIsva("0");
			FdPurchaseModel fdPurchase = new FdPurchaseModel();
			fdPurchase.setFdPoState("9");
			fdPurchase.setFdPoId(audit.getQwId());
			fdPurchaseDao.updateByPrimaryKeySelective(fdPurchase);
		}
		zgsSellOrder.setSoId(audit.getQwId());
		int rows = zgsSellOrderDao.updateByPrimaryKeySelective(zgsSellOrder);
		
		String auditId = UUID.randomUUID().toString().replaceAll("-", "");
		audit.setAuditId(auditId);
		audit.setFilId("1");
		int rowsTwo = auditDao.insertSelective(audit);
	}

	@Override
	public int findSoId(String SoId) {
		
		return zgsSellOrderDao.findSoId(SoId);
	}
	
}
